package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.queryObject.RegionQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.sun.tracing.dtrace.ModuleAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("region")
public class RegionController {

    @Autowired
    private IRegionService regionService;


    /**
     * 分页映射方法
     * @param model
     * @param qo    分页条件
     * @return
     */
    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo")RegionQueryObject qo){
        // Page : spring-data 提供用于 mongodb 分页的类
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page",page);
        return "region/list";
    }


    /**
     * 保存或更新
     * @param region    要保存的数据
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        if (region.getId()!=null){
            // 如果id不为空,那么作更新操作
            regionService.update(region);
        }else {
            // 保存
            regionService.save(region);
        }
        return JsonResult.success();
    }

    /**
     * 目的地关联地区查看
     * @param rid 要查看关联地区的区域 id
     * @return 返回关联地区的 list
     */
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        List<Destination> list = regionService.getDestByRegionId(rid);
        return list;
    }

    /**
     * 删除方法
     * @param id    要删除区域的id
     * @return  JsonResult
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }


    /**
     * 修改热门状态
     * @param hot   热门状态的值
     * @param id    要修改的区域的id
     * @return      JsonResult
     */
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot,String id){
        regionService.changeHotValue(hot,id);
        return JsonResult.success();
    }


}
